<template>
	<view class="pages-a">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view class="custom-nav__back" @click="goLink('/pageA/navigation/navigation')">
					<view class="tn-icon-deploy"></view>
				</view>
				<view class="tn-margin-top"
					style="text-shadow:  1rpx 0 0 #FFF, 0 1rpx 0 #FFF, -1rpx 0 0 #FFF , 0 -1rpx 0 #FFF;">
					<tn-tabs :list="scrollList" :current="current" @change="tabChange" activeColor="#000" :bold="true"
						:fontSize="28"></tn-tabs>
				</view>
			</view>
		</tn-nav-bar>
		<view class="" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view class="tn-color-gray--dark"
				style="margin: 20rpx 30rpx 0 30rpx;border-radius: 100rpx;padding-left: 6rpx;background-color: rgba(248, 247, 248, 0.9);"
				@click="goLink('/pageA/search/search')">
				<tn-notice-bar :list="searlist" mode="vertical" leftIconName="search" :duration="6000"></tn-notice-bar>
			</view>
			<swiper class="card-swiper" :circular="true" :autoplay="true" duration="500" interval="8000" @change="cardSwiper">
				<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
					<view class="swiper-item image-banner"
						:style="'background-image:url('+ item.url + ');background-size: cover;border-radius: 15rpx;'">
					</view>
					<view class="swiper-item-text">
						<view class="tn-text-bold tn-color-white" style="font-size: 50rpx;">{{item.title}}</view>
						<view class="tn-color-white tn-padding-top" style="font-size: 30rpx;">{{item.name}}</view>
						<view class="tn-text-sm tn-text-bold tn-color-white tn-padding-top-sm tn-padding-bottom-sm">{{item.text}}
						</view>
					</view>
				</swiper-item>
			</swiper>
			<view class="indication">
				<block v-for="(item,index) in swiperList" :key="index">
					<view class="spot" :class="cardCur==index?'active':''"></view>
				</block>
			</view>
		</view>
		<!-- 方式5，图片形式，安卓手机 tabChange -->
		<view v-if="isAndroid" class="tn-flex tn-flex-wrap tn-padding-top home-shadow">
			<block v-for="(item, index) in icons1" :key="index">
				<view class="tn-margin-bottom tn-margin-top-sm" style="width: 25%;" @click="goLink(item.url)">
					<view class="tn-flex tn-flex-direction-column tn-flex-row-center tn-flex-col-center">
						<view class="icon5__item--icon tn-flex tn-flex-row-center tn-flex-col-center"
							:style="'background-image:url('+ item.icon +');background-size:100% 100%;background-size: cover;'">
						</view>
						<view class="tn-color-black tn-text-center">
							<text class="tn-text-ellipsis">{{ item.title }}</text>
						</view>
					</view>
				</view>
			</block>
		</view>
		<!-- 方式5 end-->
		<!-- 方式5，图片形式，苹果手机 start-->
		<view v-else class="tn-flex tn-flex-wrap tn-padding-top home-shadow">
			<block v-for="(item, index) in icons2" :key="index">
				<view class="tn-margin-bottom tn-margin-top-sm" style="width: 25%;" @click="goLink(item.url)">
					<view class="tn-flex tn-flex-direction-column tn-flex-row-center tn-flex-col-center">
						<view class="icon5__item--icon tn-flex tn-flex-row-center tn-flex-col-center"
							:style="'background-image:url('+ item.icon +');background-size:100% 100%;background-size: cover;'">
						</view>
						<view class="tn-color-black tn-text-center">
							<text class="tn-text-ellipsis">{{ item.title }}</text>
						</view>
					</view>
				</view>
			</block>
		</view>
		<!-- 方式5 end-->
		<!--胶囊 banner 需要可用显示出来即可 start-->
		<!-- <view class="tn-flex tn-flex-wrap tn-padding-bottom" @click="goLink('')">
      <view class="" style="width: 100%;">
        <view class="image-piccapsule tn-shadow-blur" style="background-image:url('https://resource.tuniaokj.com/images/capsule-banner/banner-tnmb.png');">
           <view class="image-capsule">
           </view>
         </view>  
      </view>  
    </view> -->
		<!-- banner end-->
		<view class="">
			<view class="" v-if="list.length">
				<view class="" style="padding: 30rpx 20rpx;">
					<tn-waterfall ref="waterfall" v-model="list" @finish="handleWaterFallFinish">
						<template v-slot:left="{ leftList }">
							<view v-for="(item, index) in leftList" :key="item.id" class="wallpaper__item"
									@click="goLink(`/pageA/details/details?imageId=${item.id}`)">
								<view class="item__image">
									<tn-lazy-load :threshold="6000" height="100%" :image="item.mainImage" :index="item.id"
										imgMode="widthFix"></tn-lazy-load>
								</view>
								<!-- <view class="item__data">
									<view class="item__title-container">
										<text class="item__title tn-color-black">{{ item.title }}</text>
									</view>
									<view v-if="item.tags && item.tags.length > 0" class="item__tags-container">
										<view v-for="(tagItem, tagIndex) in item.tags" :key="tagIndex" class="item__tag">{{ tagItem }}
										</view>
									</view>
									<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xs">
										<view class="justify-content-item">
											<view class="tn-flex tn-flex-col-center tn-flex-row-left">
												<view class="logo-pic">
													<view class="logo-image">
														<view class=""
															:style="'background-image:url('+ item.userImage +');width: 40rpx;height: 40rpx;background-size: cover;'">
														</view>
													</view>
												</view>
												<view class="tn-padding-left-xs">
													<text class="tn-color-gray tn-text-sm">{{ item.userName }}</text>
												</view>
											</view>
										</view>
										<view class="justify-content-item">
											<text class="tn-icon-rocket tn-color-gray tn-padding-right-xs"></text>
											<text class="tn-color-gray">{{ item.viewUserCount }}</text>
										</view>
									</view>
								</view> -->
							</view>
						</template>
						<template v-slot:right="{ rightList }">
							<!-- <view class="tn-color-black tn-text-bold tn-bg-yellow home-shadow"
								style="height: 160rpx;margin: 0 10rpx 20rpx 10rpx;border-radius: 10rpx;"
								@click="goLink('/pageB/activity/activity')">
								<view class="tn-padding-left tn-padding-top-lg">
									Ai · 广场
								</view>
								<view class="tn-padding-left tn-padding-top-xs">
									晒出你的ai创作
									<text class="tn-icon-right tn-padding-left-xs"></text>
								</view>
							</view> -->
							<view v-for="(item, index) in rightList" :key="item.id" class="wallpaper__item"
								@click="goLink(`/pageA/details/details?imageId=${item.id}`)">
								<view class="item__image">
									<tn-lazy-load :threshold="6000" height="100%" :image="item.mainImage" :index="item.id"
										imgMode="widthFix"></tn-lazy-load>
								</view>
								<!-- <view class="item__data">
									<view class="item__title-container">
										<text class="item__title tn-color-black">{{ item.title }}</text>
									</view>
									<view class="item__tags-container">
										<view v-for="(tagItem, tagIndex) in item.tags" :key="tagIndex" class="item__tag">{{ tagItem }}
										</view>
									</view>
									<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xs">
										<view class="justify-content-item">
											<view class="tn-flex tn-flex-col-center tn-flex-row-left">
												<view class="logo-pic">
													<view class="logo-image">
														<view class=""
															:style="'background-image:url('+ item.userImage +');width: 40rpx;height: 40rpx;background-size: cover;'">
														</view>
													</view>
												</view>
												<view class="tn-padding-left-xs">
													<text class="tn-color-gray tn-text-sm">{{ item.userName }}</text>
												</view>

											</view>
										</view>
										<view class="justify-content-item">
											<text class="tn-icon-rocket tn-color-gray tn-padding-right-xs"></text>
											<text class="tn-color-gray">{{ item.viewUserCount }}</text>
										</view>
									</view>
								</view> -->
							</view>
						</template>
					</tn-waterfall>
				</view>
				<tn-load-more :status="loadStatus"></tn-load-more>
			</view>
			<view class="" v-else>
				<view class="" style="padding: 6vh 20rpx;">
					<view class="tn-text-center" style="font-size: 200rpx;padding-top: 30rpx;">
						<text class="tn-icon-wea-wind tn-color-gray--light"></text>
					</view>
					<view class="tn-color-gray--disabled tn-text-center tn-text-lg">内容被台风吹走了</view>
				</view>
			</view>
<!--  <view class="" v-if="current==2">
				<view class="" style="padding: 6vh 20rpx;">
					<view class="tn-text-center" style="font-size: 200rpx;padding-top: 30rpx;">
						<text class="tn-icon-wea-wind tn-color-gray--light"></text>
					</view>
					<view class="tn-color-gray--disabled tn-text-center tn-text-lg">内容被台风卷走了</view>
				</view>
			</view> -->
		</view>
		<!-- <view class='tn-tabbar-height'></view> -->
	</view>
</template>

<script>
	import { getHomeWallpaperTypeApi,getWallpaperTypeDataApi } from '@/api/api.js'
	import  homeDataArr  from "@/libs/pageA.js"
	export default {
		name: 'PagesA',
		data() {
			return {
				icons1: [{
						icon: require("../images/A1.png"),
						title: "人物写真",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A2.png"),
						title: "动漫壁纸",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A3.png"),
						title: "风景系列",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A4.png"),
						title: "安卓专属",
						url: "/pageB/wallpaper/wallpaper"
					}
				],
				icons2: [{
						icon: require("../images/A1.png"),
						title: "人物写真",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A2.png"),
						title: "动漫壁纸",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A4.png"),
						title: "风景系列",
						url: "/pageB/wallpaper/wallpaper"
					},
					{
						icon: require("../images/A3.png"),
						title: "苹果专属",
						url: "/pageB/wallpaper/wallpaper"
					}
				],
				searlist: [
					'凶姐精美壁纸',
					'情侣聊天背景',
					'热血动漫头像',
					'手机屏保壁纸'
				],
				listTatol:0,
				cardCur: 0,
				isAndroid: true,
				swiperList: [{
					id: 0,
					type: 'image',
					title: '合作勾搭',
					name: '作者微信 tnkewo',
					url: 'https://resource.tuniaokj.com/images/swiper/adno3.jpg',
				}, {
					id: 1,
					type: 'image',
					title: '海量分享',
					name: '总有你想不到的创意',
					url: 'https://resource.tuniaokj.com/images/swiper/adno2.jpg',
				}, {
					id: 2,
					type: 'image',
					title: '酷炫多彩',
					name: '更多彩蛋等你探索',
					url: 'https://resource.tuniaokj.com/images/swiper/adno4.jpg',
				}, {
					id: 3,
					type: 'image',
					title: '适配多端',
					name: 'APP、微信小程序、H5、Finclip',
					url: 'https://resource.tuniaokj.com/images/swiper/adno5.jpg',
				}, {
					id: 4,
					type: 'image',
					title: '',
					name: '',
					url: 'https://resource.tuniaokj.com/images/swiper/ad1.jpg',
				}],
				searchObj:{"page":1,"size":18,"img_name":"","bucket_id":"03852dc8-4c2c-4d3a-b877-0716b2ebcc93","user_id":0,"sort":"sort","order":"desc"},
				current: 0,
				scrollList: [],
				/* 瀑布流 viewUserCount */
				loadStatus: 'loadmore',
				list: [],
				data: [],
			}
		},
		// async onLoad() {
		// 	this.homeDataArr = await getHomeWallpaperTypeApi()
		// 	console.log(this.homeDataArr,123456789)
		// },
		async created() {
			var data = await getHomeWallpaperTypeApi({sort: "updatedAt"})
			console.log(data.data.items,678)
			this.scrollList = [
      ...data.data.items.map(item => {
        const obj = JSON.parse(item.config)
        for (let key in obj) {
          obj[key] = obj[key].replace(/\$\{((config).*?)\}/g, (v, key) => {
            const keys = key.split('.')
            if (keys[0] === 'config') {
              return obj[keys[1]]
            }
          })
        }
        return {
          id: item.id,
          name: item.name,
          type: item.type,
          tag: item.tag,
          config_baseUrl: obj.baseUrl
        }
      })
    ]
			const systemInfo = uni.getSystemInfoSync()
			if (systemInfo.system.toLocaleLowerCase().includes('ios')) {
				this.isAndroid = false
			} else {
				this.isAndroid = true
			}
			// tabChange
			this.tabChange()
			/* 瀑布流*/
			// this.getRandomData()
			
		},
		methods: {
			// cardSwiper
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},

			// tab选项卡切换 getRandomData
			tabChange(index) {
				if (index || index==0) {
					this.current = index
					this.list = []
					this.searchObj.bucket_id = this.scrollList[index].id
					this.searchObj.page = 1
				}
				getWallpaperTypeDataApi(this.searchObj).then(res=>{
					var resData = res.data.items.map(item => {
					  item.checked = false
					  const bk = this.scrollList.find(bu => bu.id === item.bucket_id)
					  item.mainImage = bk ? bk.config_baseUrl + item.img_url : item.img_url
					  return item
					})
					// 直接赋值 and 还是合并
					if (index || index==0) this.list =  resData 
					else this.list =  this.list.concat(resData)
					this.listTatol = res.data.total || 0
					if (this.list == res.data.total.length) {
						this.loadStatus = 'loaded'
					}
					// this.getRandomData()
				})
			},
			// 跳转
			goLink(e) {
				uni.setStorageSync("homeList", this.list)
				if (this.scrollList[this.current].id == '31c0da02-d53b-486d-9ff1-b36f78194898') {
					uni.navigateTo({
						url: e + "&typeId="+this.scrollList[this.current].id,
					});
					return
				}
				uni.navigateTo({
					url: e,
				});
			},
			/* 瀑布流 getRandomData */
			// 获取随机数据
			// getRandomData() {
			// 	this.loadStatus = 'loading'
			// 	for (let i = 0; i < this.searchObj.size; i++) {
			// 		let index = this.$t.number.randomInt(0, this.list.length - 1)
			// 		console.log(index,77777)
			// 		let item = JSON.parse(JSON.stringify(this.data[index]))
			// 		item.id = this.$t.uuid()
			// 		this.list.push(item)
			// 	}
			// },
			// 瀑布流加载完毕事件
			handleWaterFallFinish() {
				this.loadStatus = 'loadmore'
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pages-a {
		max-height: 100vh;
	}


	/* 自定义导航栏内容 start */
	.custom-nav {
		height: 100%;

		&__back {
			margin: auto 5rpx;
			font-size: 50rpx;
			margin-right: 10rpx;
			margin-left: 30rpx;
			flex-basis: 5%;
		}
	}

	/* 底部安全边距 start*/
	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
		height: calc(140rpx + constant(safe-area-inset-bottom));
	}

	/* 图标容器5 start */
	.icon5 {
		&__item {
			// width: 30%;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			padding: 0rpx;
			margin: 0rpx;
			transform: scale(1);
			transition: transform 0.3s linear;
			transform-origin: center center;

			&--icon {
				width: 96rpx;
				height: 96rpx;
				border-radius: 50%;
				margin-bottom: 18rpx;
				position: relative;
				z-index: 1;
			}
		}
	}

	/* 轮播视觉差 start */
	.card-swiper {
		height: 330rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		padding: 40rpx 30rpx 30rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		border-radius: 15rpx;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		will-change: transform;
		// overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
		will-change: transform;
	}

	.card-swiper swiper-item .swiper-item-text {
		margin-top: -220rpx;
		text-align: center;
		width: 100%;
		display: block;
		border-radius: 10rpx;
		transform: translate(100rpx, 0rpx) scale(0.9, 0.9);
		transition: all 0.6s ease 0s;
		will-change: transform;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item-text {
		margin-top: -220rpx;
		width: 100%;
		transform: translate(0rpx, 0rpx) scale(0.9, 0.9);
		transition: all 0.6s ease 0s;
		will-change: transform;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -70rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}


	/* 胶囊banner*/
	.image-capsule {
		padding: 100rpx 0rpx;
		font-size: 40rpx;
		font-weight: 300;
		position: relative;
	}

	.image-piccapsule {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 20rpx 20rpx 0 0;
	}

	/* 用户头像 start */
	.logo-image {
		width: 40rpx;
		height: 40rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 1rpx solid rgba(255, 255, 255, 0.05);
		// box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 瀑布流*/
	.wallpaper__item {
		background-color: #FFFFFF;
		overflow: hidden;
		margin: 0 10rpx;
		margin-bottom: 24rpx;
		// box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);

		.item {

			/* 图片 start */
			&__image {
				width: 100%;
				height: auto;
				background-color: #FFFFFF;
				border: 1rpx solid #F8F7F8;
				border-radius: 10rpx;
				overflow: hidden;
			}

			/* 图片 end */

			/* 内容 start */
			&__data {
				padding: 14rpx 0rpx;
			}

			/* 标题 start */
			&__title-container {
				text-align: justify;
				line-height: 38rpx;
				vertical-align: middle;
			}

			&__title {
				font-size: 30rpx;
			}

			/* 标题 end */

			/* 标签 start */
			&__tags-container {
				display: flex;
				flex-direction: row;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: flex-start;
			}

			&__tag {
				margin: 10rpx;
				color: #7C8191;
				background-color: #F3F2F7;
				padding: 4rpx 14rpx 6rpx;
				border-radius: 10rpx;
				font-size: 20rpx;

				&:first-child {
					margin-left: 0rpx !important;
				}
			}

			/* 标签 end */

			/* 内容 end */
		}
	}
</style>